package com.zzsy.shoppingpro.shoppingcart.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.home.bean.GoodsBean;
import com.zzsy.shoppingpro.shoppingcart.utils.CartProvider;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2019/8/23 0023.
 */

public class ShopCartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private CartProvider cartProvider; // 存储类
    private Context mContext; // 上下文
    private List<GoodsBean> datas;// 数据
    //合计总额
    private TextView tvShopcartTotal;
    private CheckBox checkboxAll;
    private CheckBox cb_all;


    public ShopCartAdapter( Context mContext, List<GoodsBean> datas, TextView tvShopcartTotal,CartProvider cartProvider, CheckBox checkboxAlls, CheckBox cb_all) {
        this.cartProvider = cartProvider;
        this.mContext = mContext;
        this.datas = datas;
        this.tvShopcartTotal = tvShopcartTotal;
        this.checkboxAll = checkboxAlls;
        this.cb_all = cb_all;

        //首次加载数据
        showTotalPrice();
        checkboxAll.setChecked(true);
        for (int i = 0; i < datas.size(); i++) {
            datas.get(i).setIsChildSelected(true);
        }
        showTotalPrice();

        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                GoodsBean goodsBean = datas.get(position);
                goodsBean.setIsChildSelected(!goodsBean.isChildSelected());
                notifyDataSetChanged();
                checkAll();
                showTotalPrice();
            }
        });

        //设置全选的点击事件
        checkboxAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = getCheckboxAll().isChecked();
                checkAll_none(checked);
                showTotalPrice();
            }
        });

        cb_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = getCb_all().isChecked();
                checkAll_none(checked);
                showTotalPrice();
            }
        });
    }

    public void checkAll_none(boolean checked) {
        if (datas != null && datas.size() > 0) {
            for (int i = 0; i < datas.size(); i++) {
                datas.get(i).setIsChildSelected(checked);
                checkboxAll.setChecked(checked);
                notifyItemChanged(i);
            }
        }else{
            checkboxAll.setChecked(false);
        }

    }

    /**
     * &
     * 全选
     */
    public void checkAll() {
        if(datas != null && datas.size() > 0){
            for(int i = 0; i< datas.size(); i++){
                if(!datas.get(i).isChildSelected()){
                    checkboxAll.setChecked(false);
                    cb_all.setChecked(false);
                    return;
                }else {
                    checkboxAll.setChecked(true);
                    cb_all.setChecked(true);
                }
            }
        }
    }


    public void deleteData(){
        if(datas != null && datas.size() > 0){
            for(Iterator iterator = datas.iterator();iterator.hasNext();){
                GoodsBean cart = (GoodsBean) iterator.next();
                if(cart.isChildSelected()){
                    //这行代码放在前面
                    int position = datas.indexOf(cart);
                    //删除本地缓存
                    cartProvider.deleteData(cart);
                    //删除当前内存
                    iterator.remove();
                    //刷新数据
                    notifyItemRemoved(position);
                }
            }
        }
    }

    /**
     * 首次加载数据 进行数据显示
     */
    public void showTotalPrice() {
        tvShopcartTotal.setText(getTotalPrice() + "");
    }

    /**
     * 获取总额
     * */
    private double getTotalPrice() {
        double total = 0;
        if(datas != null && datas.size() > 0){
            for(int i = 0; i < datas.size(); i ++){
                GoodsBean goodsBean = datas.get(i);
                if(goodsBean.isChildSelected()){
                    total += Double.parseDouble(goodsBean.getCover_price()) * Double.parseDouble(goodsBean.getNumber() + "");
                }
            }
        }
        return total;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mContext, R.layout.item_shop_cart,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(datas.get(position));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    // 回调点击事件的监听
    private OnItemClickListener onItemClickListener;

    /**
     * 点击事件的回调
     */
    interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设置复选框的封装
     */
    public CheckBox getCheckboxAll() {
        return checkboxAll;
    }

    public void setCheckboxAll(CheckBox checkboxAll) {
        this.checkboxAll = checkboxAll;
    }

    public CheckBox getCb_all() {
        return cb_all;
    }

    public void setCb_all(CheckBox cb_all) {
        this.cb_all = cb_all;
    }

    /**
     * 自定义内部类
     * */
    class ViewHolder extends RecyclerView.ViewHolder{

        private CheckBox cbGov;
        private ImageView ivGov;
        private TextView tvDescGov;
        private TextView tvPriceGov;
        private NumberAddSubView numberAddSubView;

        public ViewHolder(View itemView) {
            super(itemView);
            cbGov = itemView.findViewById(R.id.cb_gov);
            ivGov =  itemView.findViewById(R.id.iv_gov);
            tvDescGov = itemView.findViewById(R.id.tv_desc_gov);
            tvPriceGov =  itemView.findViewById(R.id.tv_price_gov);
            numberAddSubView = itemView.findViewById(R.id.number_add_subview);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
                        onItemClickListener.onItemClickListener(v,getLayoutPosition());
                    }
                }
            });
        }

        public void setData(final GoodsBean goodsBean) {
            cbGov.setChecked(goodsBean.isChildSelected());
            Glide.with(mContext)
                    .load(Config.BASE_URl_IMAGE + goodsBean.getFigure())
                    .into(ivGov);
            tvDescGov.setText(goodsBean.getName());
            tvPriceGov.setText("￥"+goodsBean.getCover_price());

            // 设置数字加减回调
            numberAddSubView.setValue(goodsBean.getNumber());

            numberAddSubView.setOnNumberChangeListener(new NumberAddSubView.OnNumberChangeListener() {
                @Override
                public void addNumber(View view, int value) {
                    goodsBean.setNumber(value);
                    cartProvider.updateData(goodsBean);
                    showTotalPrice();
                }

                @Override
                public void subNumber(View view, int value) {
                    goodsBean.setNumber(value);
                    cartProvider.updateData(goodsBean);
                    showTotalPrice();
                }
            });
        }
    }
}
